#
#  untitled.rb
#  
#
#  Created by Zhang Ximin on 8/4/10.
#  Copyright (c) 2010 Columbia University. All rights reserved.
#

require 'watir'
require 'mysql'
require '/Users/xz2210/Christine_Columbia/Summer_Internship_2010/shermanstravelmedia/environment/html_class_v2/classes/html.class'
require 'resolv'

# Set typing variables 
email_init = "zhangxmbxy@hotmil.com"

# Set testing environment
browser_type = 'Firefox'
#~ browser_type = 'IE'
test_environment = 'QA'

# initialize HTML report
@html = HTMLReport.new()
# createReport(reportName, header)
@report = @html.createReport('Shermanstravel_TEST', 'Shermanstravel Media General Test Report', browser_type)

# begin
begin 
  
  #==============Luxury================
  # initialization
  #site = "http://www.shermanstravel.com/"
  site = "http://widersite.qa.shermanstravel.com/luxury"
  if browser_type == 'IE'
    @browser = Watir::Browser.start(site)
  elsif browser_type == 'Firefox'
    Watir::Browser.default = 'firefox'
    @browser = Watir::Browser.start(site)
  end
  
  # flag
  flag_lux = true
  if(flag_lux)
	  # test case info
	  test_case = @html.newTestName('Verify Luxury')
	  #------------email check-------------
	  # test task info
	  test_task = 'Check email info storage'
	  # test simulation
	  @browser.text_field(:id, "txtNewsletterSignupEmail").set(email_init)
	  @browser.button(:src,"http://i.stcdn.net/btn_signmeup_light_orange.jpg").click
	  @browser.text_field(:id, "lux_my_zip").set("10025")
	  @browser.checkbox(:id, "si_1").set
	  @browser.checkbox(:id, "si_9").set
	  @browser.text_field(:id, "lux_city").set("ny")
	  @browser.text_field(:id, "lux_addr_two").set("APT 3e")
	  @browser.text_field(:id, "lux_addr_one").set("219 W 100")
	  @browser.select_list(:id, "people_address_state_or_territory").set("NJ")
	  @browser.select_list(:id, "people_address_state_or_territory").set("NY")
	  @browser.text_field(:id, "lux_zip").set("10024")
	  @browser.select_list(:id, "people_address_country_id").set("Tuvalu")
	  @browser.select_list(:id, "people_address_country_id").set("United States")
	  @browser.text_field(:id, "lux_phone").set("347-226-1816-1")
	  @browser.text_field(:id, "lux_phone").set("347-226-1816")
	  @browser.checkbox(:id, "magazine_checkbox").set
	  @browser.button(:value,"Submit").click
	  # database		
	  my = Mysql.new("db1.shermanstravel.com", "webuser", "0xQ!=_7R", 'st')
	  res = my.query("SELECT * FROM people where email = '"+email_init+"' " )  
	  # report generation
	  @html.add_to_report(!res.nil?, test_task, 'PASS: Email('+email_init+')  stored successfully', 'FAIL: Unable to store email address '+email_init+' ')
	  
	  #------------postal code check------------
	  # test task info
	  test_task = 'Check postal code storage'
	  if(!res.nil?) # There is information about the email address
		res.each do |row|
			if (!row[0].nil?)
				#res3 = my.query("SELECT * FROM people_addresses pa where pa.id = "+row['address_id'])
				res3 = my.query("SELECT pa.id FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+email_init+"' ")
				res4 = my.query("SELECT pa.postal_code FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+email_init+"' ")
				#res3 = my.query("SELECT * FROM people_addresses pa where pa.id = '"+temp_addrid+"' ")
				if(res3) # The user has submitted
					@html.add_to_report(!res4.nil?, test_task, 'PASS: Postal code corresponding to ('+email_init+') has been stored successfully(or exists before) ', 'FAIL: Unable to store postal code with '+email_init+' ')
				else # No submit of the details, no instance
					@html.add_to_report(true, test_task, 'PASS: No instance of ('+email_init+') in people_address table', 'PASS:  No instance of ('+email_init+') in people_address table')
				end
				#  res2 = my.query("SELECT postal_code FROM people p, people_addresses pa where p.email = '"+email_init+"' and p.address_id = pa.id")
			end
		end
	  else
	  @html.add_to_report(true, test_task, 'Invalid: Email address ('+email_init+') not exists', 'Invalid: Email address ('+email_init+') not exists ')
	  end
  end
  
  #=================top25====================
  # initialization
  site = "http://shermanstravel.com/top25/signup"
  email = "zhangxm@hotmail.com"
  if browser_type == 'IE'
    @browser = Watir::Browser.start(site)
  elsif browser_type == 'Firefox'
    Watir::Browser.default = 'firefox'
    @browser = Watir::Browser.start(site)
  end
  
  #--------email validation check------------
  # domain check
  
  # simulation
  @browser.text_field(:id, "primary_email").set(email)
  @browser.text_field(:id, "postal_code").set("10025")
  @browser.button(:src,"http://i.stcdn.net/top25/new_signup/subscribe.jpg").click

  #--------email storage check---------------
  
  
  #-------zip code storage check-------------
   
  
  
# teardown
  @browser.close
  @html.finishReport(@report, browser_type, test_environment)
  
  
  rescue => e
  puts $!
  puts e.backtrace
  @html.finishReport(@report, browser_type, test_environment)
  #~ @browser.close

end

